const fs = require('fs')

const pluginName = 'Plugin01';

function delDir(path){
	let files = [];
	
	if(fs.existsSync(path)){
		files = fs.readdirSync(path);
		files.forEach((file, index) => {
			let curPath = path + "/" + file;
			if(fs.statSync(curPath).isDirectory()){
				delDir(curPath); //递归删除文件夹
			} else {
				fs.unlinkSync(curPath); //删除文件
			}
		});
		fs.rmdirSync(path);
	}
	fs.mkdirSync('dist');
}
class Plugin01 {
	constructor(options) {
		this.options = JSON.parse(JSON.stringify(options));
		console.log('Plugin01', 'constructor', options);
	}
	apply(compiler) {
		const { options } = this;
    	console.log('Plugin01', 'apply', options);
		compiler.hooks.emit.tapAsync('Plugin01', (compilation, cb) => {
			console.log('Plugin01', 'run', '构建过程开始');
			delDir('./dist');//清空打包目录
			cb();
		});
	}
}

module.exports = Plugin01;